package com.banciyuan.bcywebview.biz.main.mineinfo.mywork;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.biz.circles.CircleMoreUserActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Achievement;
import de.greenrobot.daoexample.model.User;

/**
 * Created by squall on 2016/9/5.
 */
public class AchievementAdatper extends BaseAdapter {

    private Achievement achievement;
    private LayoutInflater inflater;
    private Context mContext;

    public AchievementAdatper(Achievement achievement, Context mContext) {
        this.achievement = achievement;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return achievement.getTop_record().size() + 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position > 0 && position < achievement.getTop_record().size() + 1) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    private class ViewHolder {
        public ImageView iv_icon;
        public TextView tv_work;
        public TextView rank_name;
        public TextView rank;

        public ViewHolder(View viewroot) {
            iv_icon = (ImageView) viewroot.findViewById(R.id.iv_icon);
            tv_work = (TextView) viewroot.findViewById(R.id.tv_work);
            rank_name = (TextView) viewroot.findViewById(R.id.rank_name);
            rank = (TextView) viewroot.findViewById(R.id.tv_rank);
        }
    }

    private class ViewHeader {
        public ImageView iv_header;
        public TextView iv_name;
        public TextView iv_time;

        public ViewHeader(View viewroot) {
            iv_header = (ImageView) viewroot.findViewById(R.id.iv_header);
            iv_name = (TextView) viewroot.findViewById(R.id.tv_name);
            iv_time = (TextView) viewroot.findViewById(R.id.tv_time);
        }
    }

    private class ViewBottom {
        public TextView share;

        public ViewBottom(View viewroot) {
            share = (TextView) viewroot.findViewById(R.id.tv_show_statistics);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHeader viewHeader = null;
        ViewBottom viewBottom = null;
        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = inflater.inflate(R.layout.achievement_header, null);
                    viewHeader = new ViewHeader(convertView);
                    convertView.setTag(viewHeader);
                    break;
                case 1:
                    convertView = inflater.inflate(R.layout.achievement_item, null);
                    viewHolder = new ViewHolder(convertView);
                    convertView.setTag(viewHolder);
                    break;
                case 2:
                    convertView = inflater.inflate(R.layout.achievement_footer, null);
                    viewBottom = new ViewBottom(convertView);
                    convertView.setTag(viewBottom);
                    break;
            }
        } else {
            switch (getItemViewType(position)) {
                case 0:
                    viewHeader = (ViewHeader) convertView.getTag();
                    break;
                case 1:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
                case 2:
                    viewBottom = (ViewBottom) convertView.getTag();
                    break;
            }
        }
        switch (getItemViewType(position)) {
            case 0:
                User user = achievement.getUser();
                if (viewHeader != null) {
                    ImageLoader.getInstance().displayImage(user.getAvatar(), viewHeader.iv_header, ImageOptions.optionHead);
                    if (!TextUtils.isEmpty(user.getUname()))
                        viewHeader.iv_name.setText(user.getUname());
                    if (!TextUtils.isEmpty(user.getJoin_days()))
                        viewHeader.iv_time.setText(String.format(mContext.getString(R.string.join_bcy_date), user.getJoin_days()));
                }

                break;
            case 1:
                final int index = position - 1;
                if (viewHolder != null && index < achievement.getTop_record().size()) {

                    DisplayMetrics dm = new DisplayMetrics();
                    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                    wm.getDefaultDisplay().getMetrics(dm);
                    int screenWidth = dm.widthPixels;
                    int textWidth = screenWidth - 80 - 270;
                    viewHolder.tv_work.setMaxWidth(textWidth);

                    final Achievement.Record record = achievement.getTop_record().get(index);
                    if (record.getRank() == 1) {
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                            viewHolder.iv_icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.rank_top, mContext.getTheme()));
                        } else {
                            viewHolder.iv_icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.rank_top));
                        }
                    } else {
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                            viewHolder.iv_icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.rank_other, mContext.getTheme()));
                        } else {
                            viewHolder.iv_icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.rank_other));
                        }
                    }
                    if (!TextUtils.isEmpty(record.getReal_name())) {
                        viewHolder.tv_work.setText(record.getReal_name());
                    }
                    if (!TextUtils.isEmpty(record.getType_name())) {
                        viewHolder.rank_name.setText(record.getType_name());
                    }
                    viewHolder.rank.setText(Html.fromHtml(String.format(mContext.getString(R.string.rank_index), String.valueOf(record.getRank()))));
                    ((View) (viewHolder.iv_icon.getParent())).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, CircleMoreUserActivity.class);
                            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, record.getId());
                            intent.putExtra(gotoUtil.INTENT_VALUE_TWO, record.getType());
                            mContext.startActivity(intent);
                        }
                    });
                }

                break;
            case 2:
                if (viewBottom != null) {
                    viewBottom.share.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BaseObserver.getInstance().notifyWatchers(BaseObserver.SHARE_EVENT);
                        }
                    });
                }
                break;

        }

        return convertView;
    }


}
